package web;

import mapper.StudentMapper;
import pojo.Student;
import pojo.Teacher;
import pojo.User;
import service.StudentService;
import service.TeacherService;
import service.UserService;
import service.impl.StudentServiceImpl;
import service.impl.TeacherServiceImpl;
import service.impl.UserServiceImpl;
import util.MybatisUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(
        urlPatterns = "/UserServlet",
        name = "UserServlet"
)
public class UserServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private UserService userService = new UserServiceImpl();
    private StudentService studentService = new StudentServiceImpl();
    private TeacherService teacherService = new TeacherServiceImpl();

    public UserServlet() {
        super();
    }

    /**
     * 因网页多数使用隐式传输，所以显示传输方式可以直接继承。
     * 本方法多数时间也用不到。
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().append("Served at: ").append(req.getContextPath());
    }

    /**
     * 利用隐式传输进行登录。
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter out = resp.getWriter();
        String act = req.getParameter("act");
        String path = req.getContextPath();
        User user = null;
        Student student = null;
        Teacher teacher = null;
        if (act.equals("login")) {
            user = userService.login(req.getParameter("username"), req.getParameter("password"));
            if (user != null) {
                /**
                 * 学生
                 */
                if (user.getStatus() == 0) {
                    student = studentService.login(req.getParameter("username"));
//                    student = MybatisUtil.createSqlSession().getMapper(StudentMapper.class).login(req.getParameter("username"));
                    req.getSession().setAttribute("student", student);
                }else if(user.getStatus() ==1){
                    teacher = teacherService.login(req.getParameter("username"));
                    req.getSession().setAttribute("teacher",teacher);
                }
                req.getSession().setAttribute("user", user);
                out.print("<script>alert('登陆成功');</script>");
                resp.setHeader("Refresh", "0;" + path + "/html/zhujiemian.jsp");
            } else {
                out.print("<script>alert('登陆失败，请检查后登录！');</script>");
//                    out.print("密码错误，请检查后登录！");
                resp.setHeader("Refresh", "0;" + path + /*此部分为login网页路径，相对服务器根路径*/"/html/login.jsp");
            }
        }
    }
}
